package com.taoyige.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taoyige.AppContext;
import com.taoyige.AppException;
import com.taoyige.R;
import com.taoyige.bean.Order;
import com.taoyige.bean.URLs;
import com.taoyige.common.UIHelper;
import com.taoyige.widget.LoadingDialog;

/**
 * 订单物流
 * @author zhuxun
 * @version 1.0
 * @date 2014-11-25 上午9:56:40
 */
public class OrdersStatusActivity extends BaseActivity {
	
	private AppContext appContext;
	private Handler handler;
	private LoadingDialog loading;
	
	private ImageButton returnBack;
	private ImageButton homePage;
	private TextView userHeadText;
	private LinearLayout linearlayout;
	private LinearLayout linearlayout1;
	
	private RelativeLayout orderDetail;
	
	private WebView trackInfo;
	
	private String orderId;
	private String uid;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_track_info);
		returnBack=(ImageButton)findViewById(R.id.user_hear_return_back);
		homePage=(ImageButton)findViewById(R.id.user_hear_homePage);
		userHeadText=(TextView)findViewById(R.id.user_hear_text);
		userHeadText.setText("物流详情");
		
		returnBack.setOnClickListener(returnListener);
		homePage.setOnClickListener(homeListener);
		
		orderId=getIntent().getStringExtra("orderId");
		uid=getIntent().getStringExtra("uid");
		appContext=(AppContext) getApplication();
		
		trackInfo = (WebView) findViewById(R.id.track_info_list);
		trackInfo.getSettings().setJavaScriptEnabled(true);
		trackInfo.getSettings().setSupportZoom(true);
		trackInfo.getSettings().setBuiltInZoomControls(true);
		trackInfo.getSettings().setDefaultFontSize(12);
		trackInfo.setWebViewClient(UIHelper.getWebViewClient());
		
		initData();
	}

	

	private void initData() {
		handler = new Handler() {
			public void handleMessage(Message msg) {
				if (loading != null)
					loading.dismiss();
				if (msg.what == 1 && msg.obj != null) {
					Order order=(Order)msg.obj;
//					Order order=new Order();
//					order.setExpressCompany("中通快递");
//					order.setExpressNumber(728479208976l);
					//trackInfo.loadUrl("http://m.kuaidi100.com/index_all.html?type="+order.getExpressCompany()+"&postid="+order.getExpressNumber());
					//trackInfo.loadUrl("http://m.kuaidi100.com/result.jsp?nu="+order.getExpressNumber());
					//trackInfo.loadUrl("http://192.168.1.200:3001/api/order/trackView?id=14102417014755250&uid=1-2088002318420134&token=123");
					trackInfo.loadUrl(URLs.ORDER_TRACK+"&id="+order.getId()+"&uid="+order.getUserID());
				} else if (msg.obj != null) {
					((AppException) msg.obj).makeToast(OrdersStatusActivity.this);
				}
				
			}
		};
		loading = new LoadingDialog(this);
		loading.show();
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
					Order  order= appContext.getOrderDetail(orderId,uid);
					msg.what = 1;
					msg.obj = order;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}
	
	private View.OnClickListener homeListener = new View.OnClickListener(){
		@Override
		public void onClick(View v){
			UIHelper.showHomePage(v.getContext());
		}
	};
	
	private View.OnClickListener returnListener = new View.OnClickListener(){
		@Override
		public void onClick(View v){
			UIHelper.showUser(v.getContext());
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
